<?php
/*****
 *	class Url
 *
 * description Lop chua cac ham xu ly viec chuyen trang, build url can chuyen toi 
 * @project chodientu
 * @package system.kernel 
 * @copyright (C) 2009 Peacesoft Solution System
 * @date 08/09/09
 * @version 2.0
 *****/
class Url {
	
	/***
	 *	Function name buildUrl
	 *	description Ham build ra url voi cac tham so $portal, $page, $params duoc truyen vao
	 * @param $portal - ten portal
	 * @param $page - ten page
	 * @param $params - mang cac param 		
	 * @return string url
	 * @note mac dinh $portal = $page = false, $params = array()     
		
	 ***/
	public static function buildUrl($params = array(),$portal=false,$page = false) {
		$request_string = '?';
        if ($portal)
			$request_string .= 'portal=' . $portal;
				
		if ($page)
        {
            if($request_string == "?")
                $request_string .= 'page=' . $page;
            else
                $request_string .= '&page=' . $page;
        }
		else
        {
            if($request_string == "?")
			    $request_string .= 'page=' . $_REQUEST ['page'];
            else
                $request_string .= '&page=' . $_REQUEST ['page'];
        }   
		if (sizeof ( $params ) > 0 and is_array ( $params )) {
			foreach ( $params as $param => $value ) {
				if (is_numeric ( $param )) {
					if (isset ( $_REQUEST [$value] ))
						$request_string .= '&' . $value . '=' . urlencode ( $_REQUEST [$value] );
				} else {
					$request_string .= '&' . $param . '=' . urlencode ( $value );
				}
			}
		}
        
		return $request_string;
	}
	/***
     *    Function name buildUrlAll
     *    description Ham build url voi tat ca cac tham so dang co hien thoi tren url dong thoi loai nhung param nam trong mang $arrExpelParam va them chuoi cac param duoc truyen vao $strAddParam
     * @param $arrExpelParam - Mang cac param muon loai tru trong duong dan url
     * @param $strAddParam - xau cac param them vao url co kieu (&param1=value1&param2=value2)        
     * @return - string url
     * @note Bat buoc phai truyen du 2 doi so $varName, $type
        
     ***/
    public static function buildUrlAll($arrExpelParam = array(), $strAddParam = '') {
        $url = '';
        $portal = '';
        $page = '';
        foreach ( $_GET as $key => $value ) {
            if (! in_array ( $key, $arrExpelParam )) {
                if ($key == 'page')
                    $page = $value;
                else if (! $url) {
                    $url = '?' . urlencode ( $key ) . '=' . urlencode ( $value );
                } else {
                    $url .= '&' . urlencode ( $key ) . '=' . urlencode ( $value );
                }
            }
        }
        if ($page == '')
            $page = $_GET ['page'];
        
        $arr = array ("?" );
        
        if ($url != '')
            $url = str_replace ( $arr, "?page=" . $page . "&", $url );
        else
            $url = "?page=" . $page;
        
        if ($strAddParam) {
            if ($url) {
                $url .= '&' . $strAddParam;
            } else {
                $url .= '?' . $strAddParam;
            }
        }
        unset ( $portal );
        unset ( $page );
        return $url;
    }
	/***
	 *    Function name redirectUrl
	 *    description Ham chuyen toi url duoc truyen vao hoac chuyen sang trang cung voi cac param duoc chuyen vao 
	 * @param $url - url duoc truyen vao
	 * @param $page - trang duoc truyen vao
	 * @param $params - mang cac param duoc truyen vao                
	 * @return voild
	 * @description mac dinh $url = $page = false se refesh lai trang hien tai 
	 * @description neu $url = false va $page = false se chuyen den trang hien thoi voi cac param truyen vao
	 * @description  neu ton tai $url se chuyen den url do
        
	 ***/
	static function redirectUrl($params = array(), $url = false, $page = false) {
		if ($url) {
            header( "HTTP/1.1 301 Moved Permanently" ); 
			header ( 'Location:' . $url );
			exit ();
		} else {
            header( "HTTP/1.1 301 Moved Permanently" ); 
			header ( 'Location:' . Url::buildUrl($params, '', $page ) );
			exit ();
		}
	}
    static function redirectHome(){
        $url = self::buildUrl(false,'admin','home');
        self::redirectUrl(false,ROOT_URL.$url);        
    }    
}
?>